/**
 * Created by MetaLab_K on 2016/3/31.
 */
'use strict';
angular.module('metalabPartnerApp').controller('DemandListReleasedCtrl', ['$rootScope', '$scope', '$stateParams', '$state', 'MetaResources', 'MetaSEO', 'MetaRemind',
    function ($rootScope, $scope, $stateParams, $state, resource, MetaSEO, MetaRemind) {
        if ($state.is('meta.demand.list.all')) {
            $scope.tabs[0].active = true;
        }
        else if ($state.is('meta.demand.list.released')) {
            $scope.tabs[1].active = true;
        }

        $scope.waiting = true;
        ///**数据模板
        $scope.page = {
            currentPage: 1,
            data: [],
            total: 0,
            pageNumber: 10
        };

        /**
         * @function 请求新的数据
         * @param page 跳转到第几页
         * @param area 筛选领域
         */
        var changeData = function (page, area) {
            $scope.waitingPromise = resource.demand.queryMore({
                userId: '_self',
                areas: area,
                _range: ((parseInt(page) - 1) * $scope.page.pageNumber + 1) + '_' + parseInt(page) * $scope.page.pageNumber,
                version: 'online',
                _sort: '-rank,-updatedAt'
            }, function (data, headers) {
                $scope.waiting = false;
                $scope.page.total = headers()['x-count'] ? parseInt(headers()['x-count']) : 0;
                $scope.page.currentPage = parseInt(page);
                $scope.page.data = data;
            }, function (err) {
                $scope.waiting = false;
                // MetaRemind.error('对不起', '服务器出错了，请稍后再尝试');
                console.log('error', err);
            }).$promise;
        };

        /**
         * @function 请求数据
         */
        $scope.filters[0].config.chose = $stateParams.area || '不限';
        changeData($stateParams.page, $stateParams.area);
        MetaSEO.setSEO('迈科技-满足技术需求-解决科技难题',
            '迈科技-集中展示急需解决的、真实可靠的企业需求，任何人可以免费获取需求方的联系信息，并选择您擅长的领域，主动推广自己的技术', $stateParams.areas + ',' + $stateParams.keywords);
    }]);
